//
//  HomGoodsDetailCell.m
//  测试10-20
//
//  Created by Yasin on 2018/10/20.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  多选cell 
//  h 
#import "HomGoodsSingleCell.h"
@interface HomGoodsSingleCell()
/**背景vivew*/
@property (nonatomic, strong) UIView *bgView;
/**标题*/
@property (nonatomic,  strong) UILabel *nameLb;
/**图标*/
@property (nonatomic,  strong) UIImageView *iconIv;

@end

@implementation HomGoodsSingleCell

#pragma mark - 一 init
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [self.contentView addSubview:self.bgView];
    [self.contentView addSubview:self.iconIv];
    [self.contentView addSubview:self.nameLb];
}

#pragma mark - 二 getter
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
    }
    return _bgView;
}
- (UILabel *)nameLb {
    if (!_nameLb) {
        _nameLb = [UILabel new];
        _nameLb.text = @"张三";
//        _nameLb.backgroundColor = [UIColor brownColor];
        _nameLb.font = [UIFont systemFontOfSize:14.0 * kScaleFit];
        _nameLb.numberOfLines = 0; //自动换行
    }
    return _nameLb;
}

- (UIImageView *)iconIv {
    if (!_iconIv) {
        _iconIv = [UIImageView new];
//        _iconIv.backgroundColor = [UIColor greenColor];
    }
    return _iconIv;
}

#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    [self.iconIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.centerY.equalTo(self.bgView.mas_centerY);
        make.left.equalTo(self.bgView.mas_left).offset(15);
    }];
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgView.mas_centerY);
        make.left.equalTo(self.iconIv.mas_right).offset(5);
        make.right.equalTo(self.bgView.mas_right);
        make.top.equalTo(self.bgView.mas_top).offset(2);
        make.bottom.equalTo(self.bgView.mas_bottom).offset(-2);
    }];
}

#pragma mark - 四设置数据
- (void)setModel:(HomeGoodsDetailModel *)model {
    _model = model;
 
    //2019-2-13新需求:如果价格为0  不显示价格和括号eg:Ham,
    //如果价格>0.0 显示Ham(+€1.0)
    
    NSString *nameStr = @"";
    if ([model.currentPrice floatValue] < 0.1) {
        nameStr =  [NSString stringWithFormat:@"%@",model.name];
    }else {
        nameStr = [NSString stringWithFormat:@"%@ (+€%.1f)",model.name,[model.currentPrice floatValue]];
    }
    self.nameLb.text = nameStr;
    
    //一:单选多选/ 选中 未选中判断
    if ([model.only isEqualToNumber:@1]) { //单选
        //单选只有一个被选中。需要在外部同时修改 isSelected 和 defaultSelect的值
        //isSelected YES:选中 NO未选中
        //defaultSelect 标记为@(-1)
        if (model.isSelected || [model.defaultSelect isEqualToNumber:model.tagId]) {
            self.iconIv.image = [UIImage imageNamed:@"单选-选中.png"];
        }else {
            self.iconIv.image = [UIImage imageNamed:@"单选-未选中.png"];
        }
        
    }else { //多选
        self.iconIv.image = [UIImage imageNamed:@"复选框-未选中.png"];
        if (model.isSelected) {
            self.iconIv.image = [UIImage imageNamed:@"复选框-选中.png"];
        }
    }
}
@end

